﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using JLL.China.Application;
using JLL.China.Infrastructure;
using Microsoft.Web.Mvc;
using System.Web.Security;
using System.Text.RegularExpressions;
namespace JLL.China.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private const String ReturnUrlRegexPattern = @"\?ReturnUrl=.*$";
        public MvcApplication()
        {

            PreSendRequestHeaders += MvcApplicationOnPreSendRequestHeaders;

        }
        private void MvcApplicationOnPreSendRequestHeaders(object sender, EventArgs e)
        {

            String redirectUrl = Response.RedirectLocation;

            if (String.IsNullOrEmpty(redirectUrl)
                 || !Regex.IsMatch(redirectUrl, ReturnUrlRegexPattern))
            {

                return;

            }

            Response.RedirectLocation = Regex.Replace(redirectUrl,
                                                       ReturnUrlRegexPattern,
                                                       String.Empty);
        }
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
            ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
            BootStrapper.Initialize();
            var logger = ObjectFactory.Resolve<Infrastructure.Logging.ILogger>();
            logger.Info("Application started");
           
        }
        protected void Application_Error(object sender, EventArgs e)
        {
            //Exception exc = Server.GetLastError();
            //if (exc.GetType() == typeof(HttpException))
            //{

            //    if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))
            //        return;


            //    Response.Write("<h2></h2>\n");
            //}


            //string TemplateMessage = "<h2>Global Page Error</h2>\n";
            //TemplateMessage += "<p>" + exc.Message + "</p>\n";
            //string Html = "<script>$.fancybox(\"<h2>" + exc.Message + "<br/>" + exc.HelpLink + "<br/>2:" + exc.TargetSite + "<br/>Data:" + exc.ToString() + "</h2>\");</script>";
            //Response.Write(Html);


            //Server.ClearError();
        }
    }
}